#include "SYSTEM.h"
#include "main.h"
#include "myiic.h" 	 
#include "wav.h"


_SYSTEM_State SYSTEM_State;

void memory_check_task(void *pvParameter)
{
    int free_size=0;
    int time=0;
    //xEventGroupWaitBits(SYSTEM_State.Wav_event_group, BIT0, true, false, portMAX_DELAY);
	while (1) 
    {
        vTaskDelay(5000 / portTICK_PERIOD_MS);     
        free_size = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
		ESP_LOGW("内存监控", "内部RAM剩余%d.%dKB", free_size/1024,free_size%1024);
        free_size = heap_caps_get_free_size(MALLOC_CAP_SPIRAM);
		ESP_LOGW("内存监控", "外部PSRAM剩余%d.%dkB",free_size/1024,free_size%1024);
		
        SYSTEM_State.Temp = SHT2x_GetTempPoll();
        printf("温度:%d\r\n",SYSTEM_State.Temp);
        SYSTEM_State.Humi = SHT2x_GetHumiPoll();
        printf("湿度:%d\r\n",SYSTEM_State.Humi);
 	}
	vTaskDelete(NULL);
}


static void system_shijian_timercb(void *timer)
{
    static int time=0;


	SYSTEM_State.RiQi_Data.miao += 1;
	if (SYSTEM_State.RiQi_Data.miao >= 600)
	{
		SYSTEM_State.RiQi_Data.miao = 0;

		SYSTEM_State.RiQi_Data.fen += 1;
		if (SYSTEM_State.RiQi_Data.fen >= 60)
		{
			SYSTEM_State.RiQi_Data.fen = 0;
			SYSTEM_State.RiQi_Data.shi += 1;
			if (SYSTEM_State.RiQi_Data.shi >= 12)
				SYSTEM_State.RiQi_Data.shi = 0;
		}			
	}

    ZhuJieMian_Data.Miao = SYSTEM_State.RiQi_Data.miao;
    ZhuJieMian_Data.Fen = SYSTEM_State.RiQi_Data.fen;
    ZhuJieMian_Data.Shi = SYSTEM_State.RiQi_Data.shi;  

    if(++time>60*10*10)
    {
        time=0;
        SYSTEM_State.HuoQu_ShiJian_Flag=true;//同步一次时间
        xSemaphoreGive(SYSTEM_State.https_request_Semaphore);//释放信号量
    }

}

int SYSTEM_INIT(void)
{
    BaseType_t task_err_code;

    int ret=0;
    SYSTEM_State.https_request_Semaphore = xSemaphoreCreateCounting(10,0);//动态创建一个计数型信号量

    SYSTEM_State.wifi_event_group = xEventGroupCreate();
    SYSTEM_State.Wav_event_group = xEventGroupCreate();
    SYSTEM_State.TianQi_event_group = xEventGroupCreate();

//----时间更新定时器----//
    esp_timer_create_args_t timer_conf = 
    {
        .callback = system_shijian_timercb,
        .name     = "system_shijian_timer"
    };
    esp_timer_handle_t g_wifi_connect_timer = NULL;
    esp_timer_create(&timer_conf, &g_wifi_connect_timer);
    esp_timer_start_periodic(g_wifi_connect_timer, 100 * 1000U);
//----时间更新定时器----//


    //task_err_code = xTaskCreate(memory_check_task,"memory_check_task",1024*2,NULL,2,NULL);
    
    //ret = sd_card_init();
    sprintf(SYSTEM_State.bilibili_Data.id,"%s",HTTPS_bilibili_id);
    SYSTEM_Set_wifi_kaiguan(true);
	SYSTEM_State.RiQi_Data.nian=2021;
	SYSTEM_State.RiQi_Data.yue=1;
	SYSTEM_State.RiQi_Data.ri=14;

    if(SYSTEM_State.sd_sta==ESP_OK)
    {
        SD_TEST();       
        //i2s_test();
    }
    return 0;
}



int SYSTEM_Set_Language(int dat)
{
	int Language = SYSTEM_State.Language;
	SYSTEM_State.Language = dat;
	return Language;
}

int SYSTEM_Get_Language(void)
{

	return SYSTEM_State.Language;
}
int SYSTEM_Set_wifi_kaiguan(int dat)
{
	int dat1 = SYSTEM_State.wifi_kaiguan;
	SYSTEM_State.wifi_kaiguan = dat;
	return dat1;
}

int SYSTEM_Get_wifi_kaiguan(void)
{

	return SYSTEM_State.wifi_kaiguan;
}